#!/usr/bin/env python
# encoding: utf-8
"""
TwoPartySimulation.py

Created by Drew Conway <agc282@nyu.edu> on 2008-10-07.
Copyright (c) 2008. All rights reserved, see GNU LGPL.

PyVote is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.


Version = 0.1a
"""

import sys
import os
from TwoPartyLegislature_0_1 import *
from networkx import Graph,UbiGraph
from scipy import random
import pylab as P

# Creates network object of voting results (Requires NetworkX)
def create_voting_network(legislature,passed_only=False):
	G=Graph()
	num_legislators=legislature.size()
	if passed_only:
		for b in legislature.get_passed_bills():
			for a in legislature.legislators():
				r=a.get_record()
				if r[b.get_bill_num()]>0:
					G.add_edge(a.id,num_legislators+b.get_bill_num())
	else:
		for a in legislature.legislators():
			r=a.get_record()
			for b in r.keys():
				if r[b]>0:
					G.add_edge(a.id,num_legislators+b)
	return G
			
	
	
if __name__ == '__main__':
	num_agents=303
	num_votes=100
	# Change the default tolerance function
	#new_tol= lambda t: 0.5*t
	
	# Random idelogical distribution
	ran_dist=random.normal(0.1,size=num_agents)
	dist_hist=P.histogram(ran_dist)[0]
	
	#L=TwoPartyLegislature(num_agents,dist_hist)
	L=TwoPartyLegislature(num_agents)
	L.info()

	# Begin voting procedure
	for v in range(0,num_votes):
		C=Committee(L,11)	# Create committee
		B=C.make_bill(majority_rule=False)	# Committee creates bill
		B.set_bill_num(v)
		for a in L.legislators():
		# Now ask each legislator if it will vote for the bill
			if a.get_party()<1:
				if B.get_ideology()<=a.get_tolerance():
					a.add_record(v,1)
					B.add_record(a,1)
				else:
					a.add_record(v,0)
					B.add_record(a,0)
			else:
				if B.get_ideology()>=a.get_tolerance():
					a.add_record(v,1)
					B.add_record(a,1)
				else:
					a.add_record(v,0)
					B.add_record(a,0)
		if (B.num_conservative_yays()+B.num_liberal_yays())>(num_agents/2):
			B.set_passage(True)
		else:
			B.set_passage(False)
		L.add_bill(B)
		
	passed=L.get_passed_bills()
	num_bills_passed=len(passed)
	
	# Basic Output
	if num_bills_passed>0:
		print 'Number of bills passed '+str(len(passed))
		print '-- Records of bills passed --'
		print ''
		for b in passed:
			b.info()
			print ''
	else:
		print 'No bills passed :-('
		print ''
	
	'''
	# Fancy Ouput
	U=UbiGraph(create_voting_network(L,passed_only=True))
	
	blue_nodes=U.new_node_style(color='#0000FF')
	green_cubes=U.new_node_style(color='#008000',shape='cube')
	libs=L.find_liberals()
	
	bill_index=range(num_agents,num_agents+num_votes)
	U.set_node_attr(libs,style=blue_nodes)
	U.set_node_attr(bill_index,style=green_cubes)
	
	L.show_ideolody_distribution()
	L.show_tolerance_distribution()
	L.show_tolerance_scatter()
	P.show()
	
	print '--- Full legislative record ---'
	print ''
	for b in L.get_all_bills():
		b.info()
		print ''
	'''